<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decorations">
      <div class="floating-element element-1"></div>
      <div class="floating-element element-2"></div>
      <div class="floating-element element-3"></div>
      <div class="floating-element element-4"></div>
      <div class="floating-element element-5"></div>
      <div class="floating-element element-6"></div>
      <div class="floating-element element-7"></div>
      <div class="floating-element element-8"></div>
      <div class="floating-element element-9"></div>
      <div class="floating-element element-10"></div>
      <div class="floating-element element-11"></div>
      <div class="floating-element element-12"></div>
    </div>

    <div class="login-content">
      <!-- 左侧登录表单 -->
      <div class="login-form-section">
        <div class="login-card">

          <!-- 标题 -->
          <h1 class="login-title">欢迎登陆！</h1>
          <p class="platform-subtitle">{{ appName }}</p>

          <!-- 表单 -->
          <form @submit.prevent="handleLogin" class="login-form">
            <div class="form-group">
              <label class="form-label">Username</label>
              <input
                type="text"
                v-model="form.username"
                class="form-input"
                placeholder="Username"
                required
              />
            </div>

            <div class="form-group">
              <label class="form-label">Password</label>
              <input
                type="password"
                v-model="form.password"
                class="form-input"
                placeholder="Password"
                required
              />
            </div>

            <div class="forgot-password">
              <a href="#" class="forgot-link">Forgot Password?</a>
            </div>

            <button type="submit" class="sign-in-btn" :disabled="loading">
              {{ loading ? 'Signing in...' : 'Sign in' }}
            </button>

            <div class="register-section">
              <span>还没有帐号？ </span>
              <a href="#" class="register-link">点击注册>></a>
            </div>
            
            <!-- 灰度模式提示 -->
            <div v-if="isGrayMode" class="gray-mode-tip">
              <div class="tip-icon">🟡</div>
              <div class="tip-text">
                <strong>灰度模式</strong><br>
                已自动填充登录信息，可直接登录
              </div>
            </div>
          </form>
        </div>
      </div>


    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { useAuthStore } from '../stores/auth'
import { authAPI } from '../utils/api'
import { APP_CONFIG, ROUTES_CONFIG, GRAY_MODE_CONFIG } from '../config'

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const authStore = useAuthStore()
    const loading = ref(false)
    const isGrayMode = ref(GRAY_MODE_CONFIG.enabled)
    
    const form = reactive({
      username: '',
      password: ''
    })

    // 灰度模式下自动填充账号密码
    onMounted(() => {
      if (isGrayMode.value) {
        form.username = GRAY_MODE_CONFIG.username
        form.password = GRAY_MODE_CONFIG.password
      }
    })

    const handleLogin = async () => {
      if (!form.username || !form.password) {
        alert('请输入用户名和密码')
        return
      }

      loading.value = true

      try {
        // 使用API调用进行登录
        const response = await authAPI.login({
          username: form.username,
          password: form.password
        })
        
        if (response.success) {
          // 登录成功，更新用户状态
          const userInfo = {
            id: response.user?.id || '001',
            username: response.user?.username || form.username,
            email: response.user?.email || 'test@company.com',
            role: '高级测试开发工程师',
            joinDate: '2023-01-15',
            activeProjects: 12,
            monthlyExecutions: 156,
            passRate: '89%',
            mode: response.user?.mode || 'normal'
          }
          
          userStore.login(userInfo)
          
          // 同时更新authStore以确保路由守卫正常工作
          await authStore.login({
            token: 'mock-token-' + Date.now(),
            userInfo: userInfo
          })
          
          // 灰度模式提示
          if (response.user?.mode === 'gray') {
            console.log('🟡 灰度模式登录成功')
          }
          
          setTimeout(() => {
            console.log('🚀 即将跳转到主页:', ROUTES_CONFIG.main)
            router.push(ROUTES_CONFIG.main)
            loading.value = false
          }, 1000)
        } else {
          alert(response.message || '登录失败')
          loading.value = false
        }
      } catch (error) {
        // 简单验证作为后备方案
        if (form.username === 'admin' && form.password === '123456') {
          // 登录成功，更新用户状态
          userStore.login({
            id: '001',
            username: '冰酱',
            email: 'test@company.com',
            role: '高级测试开发工程师',
            joinDate: '2023-01-15',
            activeProjects: 12,
            monthlyExecutions: 156,
            passRate: '89%',
            mode: 'gray'
          })
          
          // 同时更新authStore
          await authStore.login({
            token: 'mock-token-' + Date.now(),
            userInfo: {
              id: '001',
              username: '冰酱',
              email: 'test@company.com',
              role: '高级测试开发工程师',
              mode: 'gray'
            }
          })
          
          console.log('🟡 灰度模式本地登录成功')
          
          setTimeout(() => {
            console.log('🚀 本地验证跳转到主页:', ROUTES_CONFIG.main)
            router.push(ROUTES_CONFIG.main)
            loading.value = false
          }, 1000)
        } else {
          console.error('API调用失败:', error)
          alert('用户名或密码错误')
          loading.value = false
        }
      }
    }

    return {
      form,
      loading,
      isGrayMode,
      handleLogin,
      appName: APP_CONFIG.name,
      grayModeInfo: GRAY_MODE_CONFIG
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f3e8ff 0%, #e9d5ff 30%, #ddd6fe 60%, #c4b5fd 100%);
  position: relative;
  overflow: hidden;
}

.background-decorations {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.floating-element {
  position: absolute;
  border-radius: 50%;
}

.element-1 {
  animation: float 6s ease-in-out infinite;
}

.element-2 {
  animation: floatSlow 8s ease-in-out infinite;
}

.element-3 {
  animation: floatFast 4s ease-in-out infinite;
}

.element-4 {
  animation: pulse 5s ease-in-out infinite;
}

.element-5 {
  animation: float 7s ease-in-out infinite;
}

.element-6 {
  animation: floatSlow 9s ease-in-out infinite;
}

.element-7 {
  animation: floatFast 3.5s ease-in-out infinite;
}

.element-8 {
  animation: pulse 4.5s ease-in-out infinite;
}

.element-9 {
  animation: float 5.5s ease-in-out infinite;
}

.element-10 {
  animation: floatSlow 7.5s ease-in-out infinite;
}

.element-11 {
  animation: floatFast 6.5s ease-in-out infinite;
}

.element-12 {
  animation: pulse 8.5s ease-in-out infinite;
}

.element-1 {
  width: 140px;
  height: 140px;
  background: linear-gradient(135deg, #a855f7, #8b5cf6);
  top: 15%;
  left: 10%;
  animation-delay: 0s;
}

.element-2 {
  width: 120px;
  height: 120px;
  background: linear-gradient(135deg, #ec4899, #f472b6);
  top: 20%;
  right: 15%;
  animation-delay: 2s;
}

.element-3 {
  width: 90px;
  height: 90px;
  background: linear-gradient(135deg, #06b6d4, #0891b2);
  bottom: 25%;
  left: 15%;
  animation-delay: 4s;
}

.element-4 {
  width: 130px;
  height: 130px;
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  bottom: 20%;
  right: 20%;
  animation-delay: 1s;
}

.element-5 {
  width: 110px;
  height: 110px;
  background: linear-gradient(135deg, #f59e0b, #d97706);
  top: 35%;
  left: 5%;
  animation-delay: 2.5s;
}

.element-6 {
  width: 85px;
  height: 85px;
  background: linear-gradient(135deg, #10b981, #059669);
  top: 70%;
  right: 8%;
  animation-delay: 3.5s;
}

.element-7 {
  width: 125px;
  height: 125px;
  background: linear-gradient(135deg, #f472b6, #ec4899);
  bottom: 35%;
  left: 25%;
  animation-delay: 4.5s;
}

.element-8 {
  width: 75px;
  height: 75px;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  top: 50%;
  right: 30%;
  animation-delay: 5.5s;
}

.element-9 {
  width: 100px;
  height: 100px;
  background: linear-gradient(135deg, #ef4444, #dc2626);
  bottom: 50%;
  right: 5%;
  animation-delay: 6s;
}

.element-10 {
  width: 115px;
  height: 115px;
  background: linear-gradient(135deg, #06b6d4, #0891b2);
  top: 25%;
  left: 30%;
  animation-delay: 1.5s;
}

.element-11 {
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #8b5cf6, #a855f7);
  bottom: 15%;
  left: 8%;
  animation-delay: 7s;
}

.element-12 {
  width: 135px;
  height: 135px;
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  top: 60%;
  left: 12%;
  animation-delay: 0.5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.6;
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
    opacity: 0.9;
  }
}

@keyframes floatSlow {
  0%, 100% {
    transform: translateY(0px) scale(1);
    opacity: 0.5;
  }
  50% {
    transform: translateY(-30px) scale(1.1);
    opacity: 0.8;
  }
}

@keyframes floatFast {
  0%, 100% {
    transform: translateY(0px) rotate(0deg) scale(1);
    opacity: 0.7;
  }
  33% {
    transform: translateY(-15px) rotate(120deg) scale(0.9);
    opacity: 0.9;
  }
  66% {
    transform: translateY(-25px) rotate(240deg) scale(1.1);
    opacity: 0.6;
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.9;
  }
}

.login-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  position: relative;
  z-index: 2;
  padding: 2rem;
}

.login-form-section {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 600px;
}

.login-card {
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(30px);
  border-radius: 28px;
  padding: 4rem;
  width: 100%;
  max-width: 500px;
  box-shadow: 
    0 25px 50px rgba(139, 92, 246, 0.2),
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    inset 0 -1px 0 rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.4);
  position: relative;
  margin: 0 auto;
}

.login-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, 
    rgba(255, 255, 255, 0.3) 0%, 
    rgba(255, 255, 255, 0.1) 50%, 
    rgba(255, 255, 255, 0.05) 100%);
  border-radius: 24px;
  pointer-events: none;
  z-index: -1;
}

.logo-section {
  margin-bottom: 1rem;
}

.logo-text {
  color: #8b5cf6;
  font-size: 1.125rem;
  font-weight: 500;
}

.login-title {
  font-size: 1.8rem;
  font-weight: 700;
  color: #333333;
  margin-bottom: 2.5rem;
  line-height: 1.2;
  text-align: margin-left;
}

.platform-subtitle {
  color: #8b5cf6;
  font-weight: 800;
  font-size: 1.8rem;
  text-align: margin-left;
  margin-bottom: 2rem;
  margin-top: -1rem;
}

.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 2rem;
}

.form-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  color: #333333;
  margin-bottom: 0.5rem;
}

.form-input {
  width: 100%;
  padding: 1rem 1.25rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 14px;
  font-size: 1rem;
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(10px);
  color: #6b7280;
  transition: all 0.3s ease;
}

.form-input:focus {
  outline: none;
  color: #1f2937;
  background: rgba(255, 255, 255, 0.6);
  border-color: rgba(139, 92, 246, 0.5);
  box-shadow: 
    0 0 0 3px rgba(139, 92, 246, 0.1),
    0 8px 25px rgba(139, 92, 246, 0.15);
}

.form-input::placeholder {
  color: #BFBFBF;
}

.forgot-password {
  text-align: right;
  margin-bottom: 1.5rem;
}

.forgot-link {
  color: #7c3aed;
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  letter-spacing: 0.7px;
}

.forgot-link:hover {
  text-decoration: underline;
}

.sign-in-btn {
  width: 100%;
  background: linear-gradient(135deg, 
    rgba(139, 92, 246, 0.9) 0%, 
    rgba(124, 58, 237, 0.9) 100%);
  backdrop-filter: blur(10px);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 1.125rem;
  border-radius: 14px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 2rem;
  position: relative;
  overflow: hidden;
}

.sign-in-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, 
    transparent, 
    rgba(255, 255, 255, 0.2), 
    transparent);
  transition: left 0.5s;
}

.sign-in-btn:hover::before {
  left: 100%;
}

.sign-in-btn:hover:not(:disabled) {
  background: #7c3aed;
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(139, 92, 246, 0.3);
}

.sign-in-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.divider {
  text-align: center;
  margin: 1.5rem 0;
  position: relative;
  color: #333333;
  font-size: 0.875rem;
  font-weight: 500;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e5e7eb;
}

.divider span {
  background: rgba(255, 255, 255, 0.95);
  padding: 0 1rem;
}

.social-buttons {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 2rem;
}

.social-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.875rem;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.4);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 600;
  color: #666;
  font-size: 1rem;
}

.social-btn:hover {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.6);
  border-color: rgba(139, 92, 246, 0.3);
  box-shadow: 
    0 8px 25px rgba(139, 92, 246, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

.register-section {
  text-align: center;
  font-size: 0.875rem;
  color: #333333;
}

.register-link {
  color: #7c3aed;
  text-decoration: none;
  font-weight: 800;
}

.register-link:hover {
  text-decoration: underline;
}

/* 灰度模式提示样式 */
.gray-mode-tip {
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
  border: 2px solid #f59e0b;
  border-radius: 12px;
  padding: 12px;
  margin-top: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  animation: fadeInUp 0.5s ease-out;
}

.tip-icon {
  font-size: 24px;
  flex-shrink: 0;
}

.tip-text {
  font-size: 13px;
  color: #92400e;
  line-height: 1.4;
}

.tip-text strong {
  color: #78350f;
}



/* 响应式设计 */
@media (max-width: 1024px) {
  .login-content {
    padding: 1.5rem;
  }
  
  .character-placeholder {
    font-size: 3rem;
  }
  
  .illustration-section {
    margin-bottom: 1.5rem;
  }
}

@media (max-width: 768px) {
  .login-content {
    padding: 1rem;
  }
  
  .login-card {
    padding: 2.5rem;
    max-width: 95%;
  }
  
  .login-title {
    font-size: 2rem;
  }
  
  .form-input {
    padding: 0.875rem 1rem;
    font-size: 0.9rem;
  }
  
  .sign-in-btn {
    padding: 1rem;
    font-size: 1rem;
  }
  
  .character-placeholder {
    font-size: 2.5rem;
  }
  
  .floating-element {
    display: none;
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 2rem;
    max-width: 100%;
    margin: 0;
  }
  
  .login-title {
    font-size: 1.75rem;
  }
  
  .illustration-section {
    margin-bottom: 1rem;
  }
}
</style>